#include "all.h"





volatile FLASH_STS FLASHStatus = FLASH_COMPL;      //Flash操作状态变量





/****************************************************************
*Name:		ReadFlashNBtye
*Function:	从内部Flash读取N字节数据
*Input:		ReadAddress：数据地址（偏移地址）ReadBuf：数据指针	ReadNum：读取字节数
*Output:	       读取的字节数  
*Author:        ValerianFan
*Date:		2014/04/09
*E-Mail:	fanwenjingnihao@163.com
*Other:		
****************************************************************/
int ReadFlashNBtye(uint32_t StartAddr,uint32_t ReadAddress, uint8_t *ReadBuf, int32_t ReadNum) 
{
        int DataNum = 0;
				ReadAddress = (uint32_t)StartAddr + ReadAddress; 
        while(DataNum < ReadNum) 
		{
           *(ReadBuf + DataNum) = *(__IO uint8_t*) ReadAddress++;
           DataNum++;
		       Delay_us(5);//必须做延迟否则数据出错
        }
        return DataNum;
}



/****************************************************************
*Name:		WriteFlashOneWord
*Function:	向内部Flash写入32位数据
*Input:		WriteAddress：数据地址（偏移地址）WriteData：写入数据
*Output:	NULL 
*Author:  ValerianFan
*Date:		2014/04/09
*E-Mail:	fanwenjingnihao@163.com
*Other:		
****************************************************************/

void Write_Flash_Buff(uint32_t StartAddr,uint32_t WriteAddress,uint8_t *buffer,uint32_t length)
{
	uint32_t i, data = 0;
	
	FLASH_Unlock();
	FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR); 
	FLASHStatus = FLASH_EraseOnePage(StartAddr);
	if(FLASHStatus == FLASH_COMPL)
	{
		for(i = 0;i < length;i += 4)
		{
			data = ((uint32_t)*(buffer + i + 3) << 24) + ((uint32_t)*(buffer + i + 2) << 16) + ((uint32_t)*(buffer + i + 1) << 8) +  ((uint32_t)*(buffer + i));
			FLASHStatus = FLASH_ProgramWord(StartAddr + WriteAddress + i, data);    //flash.c 中API函数
			Delay_us(5);//必须做延迟否则数据出错
		}
	}
	FLASH_Lock();
	Delay_ms(10);
} 			

/****************************************************************
*Name:		Write_Data_Flash_Buff
*Function:	向内部Flash写入32位数据
*Input:		WriteAddress：数据地址（偏移地址）WriteData：写入数据
*Output:	NULL 
*Author:  ValerianFan
*Date:		2014/04/09
*E-Mail:	fanwenjingnihao@163.com
*Other:		
****************************************************************/

void Write_Data_Flash_Buff(uint32_t StartAddr,uint32_t WriteAddress,uint8_t *buffer,uint32_t length)
{
	uint32_t i, data = 0;
	
	FLASH_Unlock();
	for(i = 0;i < length;i += 4)
	{
		data = ((uint32_t)*(buffer + i + 3) << 24) + ((uint32_t)*(buffer + i + 2) << 16) + ((uint32_t)*(buffer + i + 1) << 8) +  ((uint32_t)*(buffer + i));
		FLASHStatus = FLASH_ProgramWord(StartAddr + WriteAddress + i, data);    //flash.c 中API函数
		Delay_us(5);//必须做延迟否则数据出错
	}
	FLASH_Lock();
	Delay_ms(10);
} 


///****************************************************************
//*Name:	Write_Data_Flash_Addr	
//*content:向FLASH指定地址写数据			
//****************************************************************/
//void Write_Byte_Flash_Addr(uint32_t WriteAddress,uint8_t *buffer,uint32_t length)
//{
//	uint8_t FlashBuff[2048]={0};
//	uint32_t i;
//	
//	if(WriteAddress > 2048) return;
//	ReadFlashNBtye(DEV_REGR_ADDR_FLAG,0,FlashBuff,2048);
//	for(i=0;i<length;i++)
//	{
//		FlashBuff[i+WriteAddress] = buffer[i];
//	}
//	Write_Flash_Buff(DEV_ID_ADDR_START,0,FlashBuff,2048);
//	ReadFlashNBtye(DEV_ID_ADDR_START,0,FlashBuff,2048);
//	
////	for(i=0;i<2048;i++)
////	{
////		UARTx_SendData(USART2,FlashBuff[i]);
////	}
//}






















